package kr.inani.openapi.google.book;

import java.util.ArrayList;
import java.util.List;

import kr.inani.openapi.Book;

import com.google.gson.Gson;

public class JsonResult
{
	private final Result result;
	private final List<Book>	bookList	= new ArrayList<Book>();
	
	public JsonResult(String responseString)
	{
		result = new Gson().fromJson(responseString, Result.class);
		if(result.items!=null) {
			for(int i=0;i<result.items.length;i++) {
				bookList.add(new GoogleBook(result.items[i]));
			}
		}
	}

	public int getTotalItems()
	{
		return result.totalItems;
	}

	public int getDisplay()
	{
		if(result.items!=null) {
			return result.items.length;
		}
		return 0;
	}

	public List<Book> getBookList()
	{
		return bookList;
	}
}

class Result
{
	public String kind;
	public int totalItems;
	public Item[] items;
}

class Item
{
	public String kind;
	public String id;
	public String etag;
	public String selfLink;
	public VolumeInfo volumeInfo;
	public SaleInfo saleInfo;
	public AccessInfo accessInfo;
	public SearchInfo searchInfo;
	
	public String getTitle()
	{
		return volumeInfo.title;
	}

	public String getLink()
	{
		return volumeInfo.infoLink;
	}

	public String getImageUrl()
	{
		return volumeInfo.imageLinks.thumbnail;
	}

	public String getAuthor()
	{
		String authors = "";
		if(volumeInfo.authors!=null) {
			for(int i=0;i<volumeInfo.authors.length;i++) {
				authors += (volumeInfo.authors[i]+",");
			}
		}
		return authors;
	}

	public String getPrice()
	{
		return "0";
	}

	public String getDiscount()
	{
		return "0";
	}

	public String getPublisher()
	{
		if(volumeInfo.publisher==null) {
			return "";
		}
		return volumeInfo.publisher;
	}

	public String getPubDate()
	{
		if(volumeInfo.publishedDate==null) {
			return "";
		}
		return volumeInfo.publishedDate;
	}

	public String getIsbn()
	{
		if(volumeInfo.industryIdentifiers!=null) {
			String isbns = "";
			for(int i=0;i<volumeInfo.industryIdentifiers.length;i++) {
				isbns+=volumeInfo.industryIdentifiers[i].identifier;
				if(i!=(volumeInfo.industryIdentifiers.length-1)) {
					isbns+=",";
				}
			}
			return isbns;
		}
		return "";
	}

	public String getDescription()
	{
		if(volumeInfo.description==null) {
			return "";
		}
		return volumeInfo.description;
	}
}

class VolumeInfo
{
	public String title;
	public String [] authors; 
	public String publisher;
	public String publishedDate;
	public String description;
	public Isbn [] industryIdentifiers;
	public int pageCount;
	public String printType;
	public String [] categories;
	public double averageRating;
	public int ratingsCount;
	public String contentVersion;
	public ImageLinks imageLinks;
	public String language;
	public String previewLink;
	public String infoLink;
	public String canonicalVolumeLink;
}

class Isbn
{
	public String type;
	public String identifier;
}

class ImageLinks
{
	public String smallThumbnail;
	public String thumbnail;
}

class SaleInfo
{
	public String country;
	public String saleability;
	public boolean isEbook;
}

class AccessInfo
{
	public String country;
	public String viewability;
	public boolean embeddable;
	public boolean publicDomain;
	public String textToSpeechPermission;
	public Epub epub;
	public Pdf pdf;
	public String webReaderLink;
	public String accessViewStatus;
}

class Epub
{
	public boolean isAvailable;
}

class Pdf
{
	public boolean isAvailable;
}

class SearchInfo
{
	public String textSnippet;
}